#include "button.h"
#include <allegro5/allegro_primitives.h>
#include "buttonskin.h"
using namespace boost;

Button::Button(ButtonSkin *pSkin, Point start)
     : View(Rect(start.x(), start.y(), pSkin->width(), pSkin->height()), NULL),
       m_pSkin(pSkin), m_buttonDown(false) {
}

void Button::paint(bool all) {
     if (m_buttonDown) {
	  al_draw_filled_rectangle(m_rect.x(), m_rect.y(), 
				   m_rect.x() + m_rect.width(), 
				   m_rect.y() + m_rect.height(), 
				   m_pSkin->downColor());
     }
     al_draw_bitmap(m_pSkin->bitmap(), m_rect.x(), m_rect.y(), 0);
     m_needPaint = false;
}

void Button::mouseDown(Point p, int button) {
     m_buttonDown = true;
     refresh();
}

void Button::mouseUp(Point p, int button) {
     m_buttonDown = false;
     m_onClick();
     refresh();
}

void Button::mouseExit(Point p) {
     if (m_buttonDown) {
	  m_buttonDown = false;
	  refresh();
     }
}

signals2::connection Button::onClick(const OnClick::slot_type &callback) {
     return m_onClick.connect(callback);
}
